
<!DOCTYPE html>
<html lang="en" class="loading">
<head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
    <meta name="viewport" content="width=device-width, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no">
    <title>02_程序结构 - fubaisen</title>
    <meta name="apple-mobile-web-app-capable" content="yes" />
    <meta name="apple-mobile-web-app-status-bar-style" content="black-translucent">
    <meta name="google" content="notranslate" />
    <meta name="keywords" content="Fechin,"> 
    <meta name="description" content="二、程序的结构MATLAB的流程控制分为： 书序结构、选择结构、循环结构
1、顺序结构顺序结构是指按照程序中语句的排列的顺序依次执行程序

例：已知三角形分别为a&amp;#x3D;4.5 ,b&amp;#x3D;,"> 
    <meta name="author" content="John Doe"> 
    <link rel="alternative" href="atom.xml" title="fubaisen" type="application/atom+xml"> 
    <link rel="icon" href="/img/favicon.png"> 
    
    
    
    <meta name="twitter:card" content="summary"/>
    <meta name="twitter:title" content="02_程序结构 - fubaisen"/>
    <meta name="twitter:description" content="二、程序的结构MATLAB的流程控制分为： 书序结构、选择结构、循环结构
1、顺序结构顺序结构是指按照程序中语句的排列的顺序依次执行程序

例：已知三角形分别为a&amp;#x3D;4.5 ,b&amp;#x3D;,"/>
    
    
    
    
    <meta property="og:site_name" content="fubaisen"/>
    <meta property="og:type" content="object"/>
    <meta property="og:title" content="02_程序结构 - fubaisen"/>
    <meta property="og:description" content="二、程序的结构MATLAB的流程控制分为： 书序结构、选择结构、循环结构
1、顺序结构顺序结构是指按照程序中语句的排列的顺序依次执行程序

例：已知三角形分别为a&amp;#x3D;4.5 ,b&amp;#x3D;,"/>
    
<link rel="stylesheet" href="/css/diaspora.css">

    <script>window.searchDbPath = "/search.xml";</script>
    <link rel="preconnect" href="https://fonts.googleapis.com">
    <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
    <link href="https://fonts.googleapis.com/css2?family=Source+Code+Pro&display=swap" rel="stylesheet">
<meta name="generator" content="Hexo 6.3.0"></head>

<body class="loading">
    <span id="config-title" style="display:none">fubaisen</span>
    <div id="loader"></div>
    <div id="single">
    <div id="top" style="display: block;">
    <div class="bar" style="width: 0;"></div>
    <a class="iconfont icon-home image-icon" href="javascript:;" data-url="http://fubaisen.github.io"></a>
    <div title="播放/暂停" class="iconfont icon-play"></div>
    <h3 class="subtitle">02_程序结构</h3>
    <div class="social">
        <div>
            <div class="share">
                <a title="获取二维码" class="iconfont icon-scan" href="javascript:;"></a>
            </div>
            <div id="qr"></div>
        </div>
    </div>
    <div class="scrollbar"></div>
</div>

    <div class="section">
        <div class="article">
    <div class='main'>
        <h1 class="title">02_程序结构</h1>
        <div class="stuff">
            <span>四月 01, 2022</span>
            
  <ul class="post-tags-list" itemprop="keywords"><li class="post-tags-list-item"><a class="post-tags-list-link" href="/tags/MATLAB%E5%AD%A6%E4%B9%A0/" rel="tag">MATLAB学习</a></li></ul>


        </div>
        <div class="content markdown">
            <h1 id="二、程序的结构"><a href="#二、程序的结构" class="headerlink" title="二、程序的结构"></a>二、程序的结构</h1><p><strong>MATLAB的流程控制分为： 书序结构、选择结构、循环结构</strong></p>
<h2 id="1、顺序结构"><a href="#1、顺序结构" class="headerlink" title="1、顺序结构"></a>1、顺序结构</h2><p>顺序结构是指按照程序中语句的排列的顺序依次执行程序<img src="https://s2.loli.net/2022/04/01/bhZvlPSEMoR8Wgz.jpg" alt="d0934be79b83bd61ad86b20fe247fa7" style="zoom: 25%;" /></p>
<ul>
<li><p>例：已知三角形分别为a&#x3D;4.5 ,b&#x3D;5.7 ,c&#x3D;2.3 ,求三角形周长和面积</p>
<p>程序命令：</p>
<figure class="highlight matlab"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">clear;</span><br><span class="line">a=a=<span class="number">4.5</span>;b=<span class="number">5.7</span>;c=<span class="number">2.3</span>;</span><br></pre></td></tr></table></figure></li>
</ul>
<h2 id="2、选择结构"><a href="#2、选择结构" class="headerlink" title="2、选择结构"></a>2、选择结构</h2><h4 id="（1）单分支选择机构"><a href="#（1）单分支选择机构" class="headerlink" title="（1）单分支选择机构"></a>（1）单分支选择机构</h4><p>语法格式：</p>
<figure class="highlight matlab"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">if</span>     条件</span><br><span class="line">	   语句A</span><br><span class="line"><span class="keyword">else</span>   执行语句B</span><br><span class="line"><span class="keyword">end</span></span><br></pre></td></tr></table></figure>

<blockquote>
<p> 说明：当条件成立时，执行语句A,否则执行语句B</p>
<p>​				单分支结构如图所示</p>
</blockquote>
<p><img src="https://s2.loli.net/2022/04/01/sRQ21L6A9nXE5Ya.jpg" alt="1fa75f9415df2fa409a2986f8e8f315"></p>
<p>程序命令：</p>
<figure class="highlight matlab"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line">x=input(<span class="string">&#x27;亲输入x的值：&#x27;</span>);</span><br><span class="line"><span class="keyword">if</span> x==<span class="number">10</span></span><br><span class="line">    y=<span class="built_in">cos</span>(x+<span class="number">1</span>)+<span class="built_in">sqrt</span>(x*x+<span class="number">1</span>);         <span class="comment">% sqrt是平方根函数</span></span><br><span class="line"><span class="keyword">else</span></span><br><span class="line">    y=x*<span class="built_in">sqrt</span>(x+<span class="built_in">sqrt</span>(x));</span><br><span class="line"><span class="keyword">end</span></span><br><span class="line"></span><br><span class="line"><span class="built_in">disp</span>([<span class="string">&#x27;x=&#x27;</span>,num2str(x)]);            <span class="comment">% num2str 将一个数值转换为字符串</span></span><br><span class="line"><span class="built_in">disp</span>([<span class="string">&#x27;y=&#x27;</span>,num2str(y)]);</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<h4 id="（2）条件嵌套结构"><a href="#（2）条件嵌套结构" class="headerlink" title="（2）条件嵌套结构"></a>（2）条件嵌套结构</h4><ul>
<li>语法格式：</li>
</ul>
<figure class="highlight matlab"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">if</span> (表达式)</span><br><span class="line">    <span class="keyword">if</span>(表达式<span class="number">1</span>) 语句 A1</span><br><span class="line">    <span class="keyword">else</span> 语句 A2</span><br><span class="line">    <span class="keyword">elseif</span>(条件<span class="number">2</span>) 语句 B1</span><br><span class="line">    <span class="keyword">else</span> 语句B2</span><br><span class="line"><span class="keyword">end</span></span><br></pre></td></tr></table></figure>

<p><img src="https://s2.loli.net/2022/04/01/7KtMaPmX6lvBILj.jpg" alt="71b8cd1c2ec436154ca8dfffb769801"></p>
<ul>
<li><p>例：</p>
<p>输入一个字符，要求：</p>
<p>​				若为大写字母，则输出其对应的小写字母；</p>
<p>​				若为小写字母，则输出其对应的大写字母；</p>
<p>​				若为数字字符，则输出其对应的数值；</p>
<p>​				若为其他字符，则原样输出</p>
<p>程序命令：</p>
<figure class="highlight matlab"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line">c=input(<span class="string">&#x27;请输入一个字符&#x27;</span>,<span class="string">&#x27;s&#x27;</span>);</span><br><span class="line"><span class="keyword">if</span> c&gt;=<span class="string">&#x27;A&#x27;</span>&amp;c&lt;=<span class="string">&#x27;Z&#x27;</span></span><br><span class="line">    <span class="built_in">disp</span>(setstr(<span class="built_in">abs</span>(c)+<span class="built_in">abs</span>(<span class="string">&#x27;a&#x27;</span>)-<span class="built_in">abs</span>(<span class="string">&#x27;A&#x27;</span>)));  <span class="comment">% setstr() 将ASCII码值转换成字符</span></span><br><span class="line"><span class="keyword">elseif</span> c&gt;=<span class="string">&#x27;a&#x27;</span>&amp;c&lt;=<span class="string">&#x27;z&#x27;</span></span><br><span class="line">    <span class="built_in">disp</span>(setstr(<span class="built_in">abs</span>(c)-<span class="built_in">abs</span>(<span class="string">&#x27;a&#x27;</span>)+<span class="built_in">abs</span>(<span class="string">&#x27;A&#x27;</span>)))</span><br><span class="line"><span class="keyword">elseif</span> c&gt;=<span class="string">&#x27;0&#x27;</span>&amp;c&lt;=<span class="string">&#x27;9&#x27;</span></span><br><span class="line">    <span class="built_in">disp</span>(<span class="built_in">abs</span>(c)-<span class="built_in">abs</span>(<span class="string">&#x27;0&#x27;</span>))</span><br><span class="line"><span class="keyword">else</span></span><br><span class="line">    <span class="built_in">disp</span>(c)</span><br><span class="line"><span class="keyword">end</span></span><br></pre></td></tr></table></figure>
</li>
<li><p>例：</p>
<p>某商场对骨结构买的商品实行打折销售，标准如下（商品价格用 <strong>price</strong> 表示）：</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">price&lt;200       没有折扣 </span><br><span class="line">200≤price&lt;500   3%折扣 </span><br><span class="line">500≤price&lt;1000  5%折扣 </span><br><span class="line">1000≤price&lt;2500 8%折扣 </span><br><span class="line">2500≤orice&lt;5000 10%折扣 </span><br><span class="line">5000≤price      15%折扣</span><br></pre></td></tr></table></figure>

<p>要求输入所售善品价格，输出其实际销售价格</p>
<p>程序命令：</p>
<figure class="highlight matlab"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line">price=input(<span class="string">&#x27;请输入商品价格&#x27;</span>);</span><br><span class="line"><span class="keyword">if</span> price&gt;=<span class="number">200</span>&amp;price &lt;<span class="number">500</span>			 <span class="comment">%价格大于等于200但小干500 </span></span><br><span class="line">	price=price*(<span class="number">1</span><span class="number">-3</span>/<span class="number">100</span>);</span><br><span class="line"><span class="keyword">elseif</span> price&gt;=<span class="number">500</span>&amp;price&lt;<span class="number">1000</span> 		 <span class="comment">%价格大于等于500但小于1000 </span></span><br><span class="line">	price=price*(<span class="number">1</span><span class="number">-5</span>/<span class="number">100</span>);</span><br><span class="line"><span class="keyword">elseif</span> price&gt;=<span class="number">1000</span>&amp;price&lt;<span class="number">2500</span>		 <span class="comment">%价格大于等于1000但小于2500 </span></span><br><span class="line">	price=price*(<span class="number">1</span><span class="number">-8</span>/<span class="number">100</span>);</span><br><span class="line"><span class="keyword">elseif</span> price&gt;=<span class="number">2500</span>&amp;price&lt;<span class="number">5000</span> 		 <span class="comment">%价格大于等于2500但小于5000 </span></span><br><span class="line">	price=price*(<span class="number">1</span><span class="number">-10</span>/<span class="number">100</span>); </span><br><span class="line"><span class="keyword">elseif</span> price&gt;=<span class="number">5000</span> 				 	 <span class="comment">%价格大于等于5000 </span></span><br><span class="line">	price=price*(<span class="number">1</span><span class="number">-14</span>/<span class="number">100</span>); </span><br><span class="line"><span class="keyword">else</span> 								 <span class="comment">%价格小于200 </span></span><br><span class="line">	price =price; </span><br><span class="line"><span class="keyword">end</span> </span><br><span class="line">	price</span><br></pre></td></tr></table></figure></li>
</ul>
<h4 id="（3）多分支选择结构"><a href="#（3）多分支选择结构" class="headerlink" title="（3）多分支选择结构"></a>（3）多分支选择结构</h4><p>多分支结构也称为多开关选择结构</p>
<ul>
<li><p>语法格式：</p>
<figure class="highlight matlab"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">switch</span> 表达式（标量或字符串）</span><br><span class="line">    <span class="keyword">case</span> <span class="number">1</span></span><br><span class="line">        语句<span class="number">1</span></span><br><span class="line">    <span class="keyword">case</span> <span class="number">2</span></span><br><span class="line">        语句<span class="number">2</span></span><br><span class="line">    <span class="keyword">case</span> n</span><br><span class="line">        语句n</span><br><span class="line">    <span class="keyword">otherwise</span></span><br><span class="line">        语句n+<span class="number">1</span></span><br><span class="line"><span class="keyword">end</span></span><br></pre></td></tr></table></figure>

<blockquote>
<p>说明：</p>
<ul>
<li>在执行时，只执行一个<code>case</code>后的语句就会跳出<code>switch-case</code>结构</li>
<li>case子句后面的表达式不仅可以为一个标量或一个字符串,还可以为一个单元矩阵</li>
<li>如果<code>case</code>子语句后面的表达式为一个<a href="https://fubaisen.github.io/2022/03/31/MATLAB%E5%AD%A6%E4%B9%A0/%E8%A1%A5%E5%85%85%E6%96%87%E4%BB%B6/11_%E5%8A%A0%E6%B7%B1%E7%90%86%E8%A7%A3%E5%8D%95%E5%85%83%E7%9F%A9%E9%98%B5%E5%92%8C%E7%BB%93%E6%9E%84%E7%9F%A9%E9%98%B5">单元矩阵</a>，则当表达式的值等于该单元矩阵中的某个元素时，执行相应的语句组case后常量的值必须互异</li>
<li><code>otherwise</code>为可选项，如果表达式的值与列出的每种情况都不相等，则<code>switch-case</code>结构中的语句将不被执行，程序继续向下运行。</li>
</ul>
</blockquote>
</li>
</ul>
<p><img src="https://s2.loli.net/2022/04/01/pbC4DgUno82MEqJ.jpg" alt="1ff38a4048d3592905ceb80f5f59a4b"></p>
<ul>
<li><p>例：针对上一个例子，使用多分支选择结构，重新编写程序。</p>
<p>程序命令：</p>
<figure class="highlight matlab"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line">price=input(<span class="string">&#x27;请输入商品价格&#x27;</span>);</span><br><span class="line"><span class="keyword">switch</span> <span class="built_in">fix</span>(price/<span class="number">100</span>)                   <span class="comment">% fix向0靠拢取整</span></span><br><span class="line">    </span><br><span class="line">    <span class="keyword">case</span>&#123;<span class="number">0</span>,<span class="number">1</span>&#125;                           <span class="comment">% 价格小于200</span></span><br><span class="line">        rate=<span class="number">0</span>;</span><br><span class="line">    <span class="keyword">case</span>&#123;<span class="number">2</span>,<span class="number">3</span>,<span class="number">4</span>&#125;                         <span class="comment">% 价格大于等于200，但小于500</span></span><br><span class="line">        rate=<span class="number">3</span>/<span class="number">100</span>;</span><br><span class="line">    <span class="keyword">case</span> <span class="built_in">num2cell</span>(<span class="number">2</span>:<span class="number">9</span>)                  <span class="comment">% 价格大于等于500，但小于1000</span></span><br><span class="line">        rate=<span class="number">5</span>/<span class="number">100</span>;</span><br><span class="line">    <span class="keyword">case</span> <span class="built_in">num2cell</span>(<span class="number">10</span>:<span class="number">24</span>)                <span class="comment">% 价格大于等于1000，但小于2500</span></span><br><span class="line">        rate=<span class="number">8</span>/<span class="number">100</span>;</span><br><span class="line">    <span class="keyword">case</span> <span class="built_in">num2cell</span>(<span class="number">25</span>:<span class="number">49</span>)                <span class="comment">% 价格大于等于2500,但小于5000</span></span><br><span class="line">        rate=<span class="number">10</span>/<span class="number">100</span>;</span><br><span class="line">    <span class="keyword">otherwise</span>                           <span class="comment">% 价格大于等于5000</span></span><br><span class="line">        rate=<span class="number">14</span>/<span class="number">100</span>;</span><br><span class="line"><span class="keyword">end</span></span><br><span class="line">price=price*(<span class="number">1</span>-rate)</span><br></pre></td></tr></table></figure></li>
</ul>
<h2 id="3、循环结构"><a href="#3、循环结构" class="headerlink" title="3、循环结构"></a>3、循环结构</h2><p>循环结构是指当前条件满足时被重复执行的一组语句。使用循环结构是计算机解决问题的主要手段之一</p>
<p>循环结构体如图所示<img src="https://s2.loli.net/2022/04/01/XjMomdb6DNqHpLl.png" alt="image-20220401132821210" style="zoom:33%;" /></p>
<h4 id="（1）while循环"><a href="#（1）while循环" class="headerlink" title="（1）while循环"></a>（1）while循环</h4><ul>
<li><p>语法格式：</p>
<figure class="highlight matlab"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">while</span> 条件表达式</span><br><span class="line">    循环体</span><br><span class="line"><span class="keyword">end</span></span><br></pre></td></tr></table></figure>

<blockquote>
<p>说明：</p>
<ul>
<li>表达式一般由<code>逻辑运算</code>、<code>关系运算</code>以及<code>一般运算</code>组成，用于判断循环的进行或停止。</li>
<li>表达式的值可以是标量或数组，其余的所有元素为 <strong>1（真）</strong>则继续循环，直到表达式值的某元素为0（假）时停止。</li>
</ul>
</blockquote>
</li>
<li><p>例：求      **n！    &lt;    **10^50   的最大值 n  ,并输出在该值下n！</p>
<p>程序命令：</p>
<figure class="highlight matlab"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">r=<span class="number">1</span>;k=<span class="number">1</span>;</span><br><span class="line"><span class="keyword">while</span> r&lt;<span class="number">1e50</span>         <span class="comment">% 在matlab中,e代表10的多少次方,后面接的是幂指数</span></span><br><span class="line">    r=r*k;k=k+<span class="number">1</span>;</span><br><span class="line"><span class="keyword">end</span></span><br><span class="line">k=k<span class="number">-1</span>;r=r./k;k=k<span class="number">-1</span>;</span><br><span class="line"><span class="built_in">disp</span>([<span class="string">&#x27;The &#x27;</span>,num2str(k),<span class="string">&#x27;! is &#x27;</span>,num2str(r)]);</span><br></pre></td></tr></table></figure></li>
</ul>
<h4 id="（2）for循环语句"><a href="#（2）for循环语句" class="headerlink" title="（2）for循环语句"></a>（2）for循环语句</h4><ul>
<li><p>语法格式：</p>
<figure class="highlight matlab"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">for</span> 循环变量=表达式<span class="number">1</span>：表达式<span class="number">2</span>：表达式<span class="number">3</span></span><br><span class="line">	循环体</span><br><span class="line"><span class="keyword">end</span></span><br></pre></td></tr></table></figure>

<blockquote>
<p>说明：</p>
<ul>
<li><code>表达式1</code>为起始值；<code>表达式2</code>为步长，<code>步长为1</code>时，可以省略；<code>表达式3</code>位终值</li>
<li>在每一次循环中，循环变量值被指定为数组的下一列，循环语句按数组中的每一个列执行一次，常以固定的或预定的次数循环</li>
</ul>
</blockquote>
</li>
<li><p>例；已知 4×3 矩阵<img src="https://s2.loli.net/2022/04/01/W12umzYLO7AJSaV.jpg" alt="3ce9c508a7722454d51f7eb997fd093.jpg" style="zoom:33%;" />，求矩阵对应列元素的和s1、对应元素的和s2 及整个矩阵元素的和s</p>
<p>程序命令：</p>
<figure class="highlight matlab"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">clc;s1=<span class="number">0</span>;</span><br><span class="line">data=[<span class="number">12</span> <span class="number">14</span> <span class="number">14</span>;<span class="number">15</span> <span class="number">16</span> <span class="number">17</span>;<span class="number">18</span> <span class="number">19</span> <span class="number">20</span>;<span class="number">21</span> <span class="number">22</span> <span class="number">23</span>];</span><br><span class="line"><span class="keyword">for</span> k=data</span><br><span class="line">    s1=s1+k;</span><br><span class="line"><span class="keyword">end</span></span><br><span class="line">s1</span><br><span class="line">s2=sum(data)</span><br><span class="line">s=sum(sum(data))       </span><br></pre></td></tr></table></figure>

<p><a href="https://fubaisen.github.io/2022/03/31/MATLAB%E5%AD%A6%E4%B9%A0/%E8%A1%A5%E5%85%85%E6%96%87%E4%BB%B6/09_sum%E5%87%BD%E6%95%B0">关于sum</a></p>
</li>
<li><p>例：已知若一个三位整数，<code>各位数字的立方和等于该数本身</code>，则称该数为水仙花数。要求输出100~999的全部水仙花数</p>
<p>程序命令：</p>
<figure class="highlight matlab"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">for</span> m=<span class="number">100</span>:<span class="number">999</span></span><br><span class="line">    m1=<span class="built_in">fix</span>(m/<span class="number">100</span>);                           <span class="comment">% 求m的百位数字</span></span><br><span class="line">    m2=<span class="built_in">rem</span>(<span class="built_in">fix</span>(m/<span class="number">100</span>),<span class="number">10</span>);                   <span class="comment">% 求m的十位数字</span></span><br><span class="line">    m3=<span class="built_in">rem</span>(m,<span class="number">10</span>);                            <span class="comment">% 求m的个位数字</span></span><br><span class="line">    <span class="keyword">if</span> m == m1*m1*m1+m2*m2*m2+m3*m3*m3</span><br><span class="line">        <span class="built_in">disp</span>(m);</span><br><span class="line">    <span class="keyword">end</span></span><br><span class="line"><span class="keyword">end</span></span><br></pre></td></tr></table></figure>
</li>
<li><p>例：试编写循环结构程序会出多个不同中心的圆</p>
<p>程序命令：</p>
<figure class="highlight matlab"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">for</span> <span class="built_in">i</span>=<span class="number">0</span>:<span class="built_in">pi</span>/<span class="number">50</span>:<span class="number">2</span>*<span class="built_in">pi</span>     <span class="comment">% 循环变量</span></span><br><span class="line">    x=<span class="number">2</span>*<span class="built_in">sin</span>(<span class="built_in">i</span>);</span><br><span class="line">    y=<span class="number">2</span>*<span class="built_in">cos</span>(<span class="built_in">i</span>);      <span class="comment">% 圆心位置</span></span><br><span class="line">    t=<span class="number">0</span>:<span class="built_in">pi</span>/<span class="number">100</span>:<span class="number">2</span>*<span class="built_in">pi</span>;</span><br><span class="line">    xx=x+<span class="built_in">sin</span>(t);yy=y+<span class="built_in">cos</span>(t);</span><br><span class="line">    <span class="built_in">plot</span>(xx,yy);     <span class="comment">% 圆心</span></span><br><span class="line">    <span class="built_in">hold</span> on;         <span class="comment">% 保留圆形</span></span><br><span class="line"><span class="keyword">end</span></span><br></pre></td></tr></table></figure></li>
</ul>
<p>！！！</p>
<h5 id="深度学习for循环"><a href="#深度学习for循环" class="headerlink" title="深度学习for循环"></a><strong>深度学习for循环</strong></h5><p>！！！</p>
<h6 id="1）简单的for循环"><a href="#1）简单的for循环" class="headerlink" title="1）简单的for循环"></a>1）简单的for循环</h6><p>for循环用来循环处理数据。<br>例：输出1~100的和</p>
<figure class="highlight matlab"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line">clear</span><br><span class="line">sum = <span class="number">0</span>;</span><br><span class="line"><span class="keyword">for</span> <span class="built_in">i</span> = <span class="number">1</span>:<span class="number">100</span></span><br><span class="line">    sum = sum +<span class="built_in">i</span>;</span><br><span class="line"><span class="keyword">end</span></span><br><span class="line">sum</span><br><span class="line"></span><br><span class="line">sum =</span><br><span class="line"></span><br><span class="line">        <span class="number">5050</span></span><br></pre></td></tr></table></figure>

<p>解释：i从1到100，每次增加一个，该共循环100次<br>注意：分号的位置；不能使用“+&#x3D;”符号；每写一层循环，必须以end结尾</p>
<h6 id="2）带有步长的for循环"><a href="#2）带有步长的for循环" class="headerlink" title="2）带有步长的for循环"></a>2）带有步长的for循环</h6><p>例：输出1到10之间的奇数和</p>
<figure class="highlight matlab"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line">clear</span><br><span class="line">sum = <span class="number">0</span>;</span><br><span class="line"><span class="keyword">for</span> <span class="built_in">i</span> = <span class="number">1</span>:<span class="number">2</span>:<span class="number">10</span></span><br><span class="line">    sum = sum + <span class="built_in">i</span>;</span><br><span class="line"><span class="keyword">end</span></span><br><span class="line">sum</span><br><span class="line"></span><br><span class="line">sum =</span><br><span class="line"></span><br><span class="line">    <span class="number">25</span></span><br></pre></td></tr></table></figure>

<p>解释：i &#x3D; 1:2:10中间的2表示步长，表示从1到10，每次增加2，即1，3，5，7，9五个数之和；步长也可以为负数</p>
<h6 id="3）对向量的遍历"><a href="#3）对向量的遍历" class="headerlink" title="3）对向量的遍历"></a>3）对向量的遍历</h6><figure class="highlight matlab"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br></pre></td><td class="code"><pre><span class="line">&gt;&gt; clear</span><br><span class="line">&gt;&gt; A = <span class="built_in">rand</span>(<span class="number">1</span>,<span class="number">4</span>);</span><br><span class="line">&gt;&gt; <span class="keyword">for</span> <span class="built_in">i</span> = A</span><br><span class="line">       <span class="built_in">i</span></span><br><span class="line">   <span class="keyword">end</span></span><br><span class="line">&gt;&gt; A</span><br><span class="line"></span><br><span class="line"><span class="built_in">i</span> =</span><br><span class="line"></span><br><span class="line">    <span class="number">0.9572</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="built_in">i</span> =</span><br><span class="line"></span><br><span class="line">    <span class="number">0.4854</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="built_in">i</span> =</span><br><span class="line"></span><br><span class="line">    <span class="number">0.8003</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="built_in">i</span> =</span><br><span class="line"></span><br><span class="line">    <span class="number">0.1419</span></span><br><span class="line">    </span><br><span class="line">A =</span><br><span class="line"></span><br><span class="line">    <span class="number">0.9575</span>    <span class="number">0.9649</span>    <span class="number">0.1576</span>    <span class="number">0.9706</span></span><br></pre></td></tr></table></figure>

<p>解释：rand函数用于随机生成0~1之间的数，rand(1,4)生成1*4的列向量。对向量的循环遍历是依次<code>取出向量中的每一个元素</code></p>
<h6 id="4）对矩阵的遍历"><a href="#4）对矩阵的遍历" class="headerlink" title="4）对矩阵的遍历"></a>4）对矩阵的遍历</h6><figure class="highlight matlab"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br></pre></td><td class="code"><pre><span class="line">&gt;&gt; A = <span class="built_in">magic</span>(<span class="number">4</span>);</span><br><span class="line">&gt;&gt; A</span><br><span class="line"></span><br><span class="line">A =</span><br><span class="line"></span><br><span class="line">    <span class="number">16</span>     <span class="number">2</span>     <span class="number">3</span>    <span class="number">13</span></span><br><span class="line">     <span class="number">5</span>    <span class="number">11</span>    <span class="number">10</span>     <span class="number">8</span></span><br><span class="line">     <span class="number">9</span>     <span class="number">7</span>     <span class="number">6</span>    <span class="number">12</span></span><br><span class="line">     <span class="number">4</span>    <span class="number">14</span>    <span class="number">15</span>     <span class="number">1</span></span><br><span class="line"></span><br><span class="line">&gt;&gt; <span class="keyword">for</span> <span class="built_in">i</span> = A;</span><br><span class="line">       <span class="built_in">i</span></span><br><span class="line">   <span class="keyword">end</span></span><br><span class="line"></span><br><span class="line"><span class="built_in">i</span> =</span><br><span class="line"></span><br><span class="line">    <span class="number">16</span></span><br><span class="line">     <span class="number">5</span></span><br><span class="line">     <span class="number">9</span></span><br><span class="line">     <span class="number">4</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="built_in">i</span> =</span><br><span class="line"></span><br><span class="line">     <span class="number">2</span></span><br><span class="line">    <span class="number">11</span></span><br><span class="line">     <span class="number">7</span></span><br><span class="line">    <span class="number">14</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="built_in">i</span> =</span><br><span class="line"></span><br><span class="line">     <span class="number">3</span></span><br><span class="line">    <span class="number">10</span></span><br><span class="line">     <span class="number">6</span></span><br><span class="line">    <span class="number">15</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="built_in">i</span> =</span><br><span class="line"></span><br><span class="line">    <span class="number">13</span></span><br><span class="line">     <span class="number">8</span></span><br><span class="line">    <span class="number">12</span></span><br><span class="line">     <span class="number">1</span></span><br></pre></td></tr></table></figure>


<p>解释：magic(n)函数用于生成任意一行或一列或主副对角线之和相等的方阵，对矩阵的循环遍历是依次<code>取出矩阵中的每一列元素</code></p>
<h6 id="5）break和continue的使用"><a href="#5）break和continue的使用" class="headerlink" title="5）break和continue的使用"></a>5）break和continue的使用</h6><p>break用于终止离它最近的一层for循环；<br>continue用于跳过离它最近的一层for循环，接着执行下一次循环</p>
<figure class="highlight matlab"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br></pre></td><td class="code"><pre><span class="line">&gt;&gt; x = <span class="number">1</span>;</span><br><span class="line">&gt;&gt; <span class="keyword">for</span> <span class="built_in">i</span> = <span class="number">1</span>:<span class="number">2</span>:<span class="number">10</span></span><br><span class="line">       <span class="keyword">if</span> <span class="built_in">i</span> &gt; <span class="number">7</span></span><br><span class="line">          <span class="keyword">break</span></span><br><span class="line">       <span class="keyword">else</span></span><br><span class="line">          x = x* <span class="built_in">i</span></span><br><span class="line">       <span class="keyword">end</span></span><br><span class="line">   <span class="keyword">end</span></span><br><span class="line"></span><br><span class="line">x =</span><br><span class="line"></span><br><span class="line">     <span class="number">1</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line">x =</span><br><span class="line"></span><br><span class="line">     <span class="number">3</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line">x =</span><br><span class="line"></span><br><span class="line">    <span class="number">15</span>		<span class="comment">% 3×5</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line">x =</span><br><span class="line"></span><br><span class="line">   <span class="number">105</span>		<span class="comment">% 15×7</span></span><br></pre></td></tr></table></figure>

<figure class="highlight matlab"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br></pre></td><td class="code"><pre><span class="line">&gt;&gt; sum = <span class="number">0</span>;</span><br><span class="line">&gt;&gt; <span class="keyword">for</span> <span class="built_in">i</span> = <span class="number">1</span>:<span class="number">6</span></span><br><span class="line">       <span class="keyword">if</span> <span class="built_in">i</span> == <span class="number">4</span></span><br><span class="line">          <span class="keyword">continue</span></span><br><span class="line">       <span class="keyword">else</span></span><br><span class="line">          sum = sum + <span class="built_in">i</span></span><br><span class="line">       <span class="keyword">end</span></span><br><span class="line">   <span class="keyword">end</span>;</span><br><span class="line"></span><br><span class="line">sum =</span><br><span class="line"></span><br><span class="line">     <span class="number">1</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line">sum =</span><br><span class="line"></span><br><span class="line">     <span class="number">3</span>		<span class="comment">% 1+2</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line">sum =</span><br><span class="line"></span><br><span class="line">     <span class="number">6</span>		<span class="comment">% 3+3</span></span><br><span class="line">     </span><br><span class="line"><span class="comment">% i=4 continue 跳出 不执行 else下方语句 </span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line">sum =</span><br><span class="line"></span><br><span class="line">    <span class="number">11</span>		<span class="comment">% 6+5</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line">sum =</span><br><span class="line"></span><br><span class="line">    <span class="number">17</span>		<span class="comment">% 11+6</span></span><br></pre></td></tr></table></figure>

<h6 id="6）多层for循环"><a href="#6）多层for循环" class="headerlink" title="6）多层for循环"></a>6）多层for循环</h6><p>for循环可以按照需要进行嵌套。</p>
<figure class="highlight matlab"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">for</span> <span class="built_in">i</span> = <span class="number">1</span>:<span class="number">2</span>:<span class="number">6</span></span><br><span class="line">    <span class="keyword">for</span> <span class="built_in">j</span> = <span class="number">1</span>:<span class="number">3</span></span><br><span class="line">        y = <span class="built_in">i</span> * <span class="built_in">j</span></span><br><span class="line">    <span class="keyword">end</span></span><br><span class="line"><span class="keyword">end</span></span><br><span class="line"></span><br><span class="line">y =</span><br><span class="line"></span><br><span class="line">     <span class="number">1</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line">y =</span><br><span class="line"></span><br><span class="line">     <span class="number">2</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line">y =</span><br><span class="line"></span><br><span class="line">     <span class="number">3</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line">y =</span><br><span class="line"></span><br><span class="line">     <span class="number">3</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line">y =</span><br><span class="line"></span><br><span class="line">     <span class="number">6</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line">y =</span><br><span class="line"></span><br><span class="line">     <span class="number">9</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line">y =</span><br><span class="line"></span><br><span class="line">     <span class="number">5</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line">y =</span><br><span class="line"></span><br><span class="line">    <span class="number">10</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line">y =</span><br><span class="line"></span><br><span class="line">    <span class="number">15</span></span><br></pre></td></tr></table></figure>

<h4 id="（3）break语句和continue语句"><a href="#（3）break语句和continue语句" class="headerlink" title="（3）break语句和continue语句"></a>（3）break语句和continue语句</h4><p><code>break语句用于终止循环，当在循环体内执行到该语句时，程序将跳出循环，继续执行循环的下语句</code></p>
<p><code>Ccontinue语句控制跳出循环中的某些语句，当在循环体内执行到该语句时，程序将跳出循环体中所有剩下的语句,继续下一次循环</code></p>
<p><code>continue，break与if语句连用常用于for或while循环语句中</code></p>
<ul>
<li><p>例：求100~200之间能被21整除的最小整数。</p>
<figure class="highlight matlab"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">for</span> n=<span class="number">100</span>:<span class="number">200</span></span><br><span class="line">    <span class="keyword">if</span> <span class="built_in">rem</span>(n,<span class="number">21</span>)~=<span class="number">0</span>  <span class="comment">% rem函数是两个数相除之后的余数</span></span><br><span class="line">        <span class="keyword">continue</span>     <span class="comment">% 重新循环</span></span><br><span class="line">    <span class="keyword">end</span></span><br><span class="line">    n</span><br><span class="line">    <span class="keyword">break</span></span><br><span class="line"><span class="keyword">end</span></span><br></pre></td></tr></table></figure>

<p>rem<a target="_blank" rel="noopener" href="https://blog.csdn.net/jk_101/article/details/110815016?utm_medium=distribute.pc_aggpage_search_result.none-task-blog-2~aggregatepage~first_rank_ecpm_v1~rank_v31_ecpm-1-110815016.pc_agg_new_rank&utm_term=matlab%E4%B8%ADrem%E7%9A%84%E4%BD%BF%E7%94%A8&spm=1000.2123.3001.4430">使用</a></p>
</li>
<li><p>例：编写一个猜数小游戏，要求输入一个100以内的整数，允许用户猜十次。每次猜的结果由程序发出反馈。</p>
<figure class="highlight matlab"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line">a=randperm(<span class="number">100</span>,<span class="number">1</span>);   <span class="comment">% 产生一个100以内的随机整数</span></span><br><span class="line"><span class="keyword">for</span> <span class="built_in">i</span>=<span class="number">1</span>:<span class="number">5</span></span><br><span class="line">    b=input(<span class="string">&#x27;请输入一个数:\n&#x27;</span>);</span><br><span class="line">    <span class="keyword">if</span> b&gt;a</span><br><span class="line">        <span class="built_in">disp</span>(<span class="string">&#x27;太大了&#x27;</span>);</span><br><span class="line">    <span class="keyword">elseif</span> b==a</span><br><span class="line">        <span class="built_in">disp</span>(<span class="string">&#x27;猜对了，这真牛逼&#x27;</span>);</span><br><span class="line">    <span class="keyword">elseif</span> b&lt;a</span><br><span class="line">        <span class="built_in">disp</span>(<span class="string">&#x27;太小了&#x27;</span>)</span><br><span class="line">    <span class="keyword">end</span></span><br><span class="line"><span class="keyword">end</span></span><br><span class="line">fprintf(<span class="string">&#x27;正确答案是 %d\n&#x27;</span>,<span class="built_in">i</span>)</span><br></pre></td></tr></table></figure></li>
</ul>
<h4 id="（4）循环嵌套语句"><a href="#（4）循环嵌套语句" class="headerlink" title="（4）循环嵌套语句"></a>（4）循环嵌套语句</h4><p><strong>循环中还包括循环称为循环嵌套，常用格如图所示</strong></p>
<p><img src="https://s2.loli.net/2022/04/01/ptAMoyRrvzYZlhV.jpg" alt="1976323b9b568ac0411db709c7f1978"></p>
<ul>
<li><p>例：一只苹果三元，一个香蕉一元，一离0.85元一个，要求用100元买这三种水果。共100格，每种水果最少买五个，问有多少买法？可以买多少个？输出全部购买方案。</p>
<p>程序命令：</p>
<figure class="highlight matlab"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line">clc;n=<span class="number">0</span>;</span><br><span class="line"><span class="keyword">for</span> apple=<span class="number">5</span>:<span class="number">33</span></span><br><span class="line">    <span class="keyword">for</span> banana=<span class="number">5</span>:<span class="number">100</span></span><br><span class="line">        <span class="keyword">for</span> pear=<span class="number">5</span>:<span class="number">125</span></span><br><span class="line">            <span class="keyword">if</span> (apple*<span class="number">3</span> +banana+pear*<span class="number">0.8</span>==<span class="number">100</span>)&amp;(apple+banana+pear ==<span class="number">100</span>)</span><br><span class="line">                <span class="built_in">disp</span>([<span class="string">&#x27;第&#x27;</span>,num2str(n+<span class="number">1</span>),<span class="string">&#x27;种方案&#x27;</span>]);</span><br><span class="line">                <span class="built_in">disp</span>([<span class="string">&#x27;苹果=&#x27;</span>,num2str(apple)&#x27;,<span class="string">&#x27;,香蕉=&#x27;</span>,num2str(banana),<span class="string">&#x27;,梨=&#x27;</span>, num2str(pear),]);</span><br><span class="line">                n=n+<span class="number">1</span>;</span><br><span class="line">            <span class="keyword">end</span></span><br><span class="line">        <span class="keyword">end</span></span><br><span class="line">    <span class="keyword">end</span></span><br><span class="line"><span class="keyword">end</span></span><br><span class="line"><span class="built_in">disp</span>([<span class="string">&#x27;购买方案共有&#x27;</span>,num2str(n),<span class="string">&#x27;种&#x27;</span>])</span><br></pre></td></tr></table></figure></li>
</ul>
<h2 id="4、try语句"><a href="#4、try语句" class="headerlink" title="4、try语句"></a>4、try语句</h2><ul>
<li><p><strong>try语句是一种试探性语句，语法格式：</strong></p>
<figure class="highlight matlab"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">try</span> </span><br><span class="line">    语句组<span class="number">1</span></span><br><span class="line"><span class="keyword">catch</span></span><br><span class="line">    语句组<span class="number">2</span></span><br><span class="line"><span class="keyword">end</span></span><br></pre></td></tr></table></figure>

<blockquote>
<p>说明：</p>
<p>try语句先试探性执行语句组 1 ，如果语句组  1 在执行过程中出现错误，则将错误信息由   <strong>catch</strong> 捕捉。执行语句 2</p>
</blockquote>
</li>
<li><p>例：矩阵乘法运算要求两矩阵的尾数兼容，否则会出错。先求两矩阵的乘积，若出错则自动转去。求两矩阵的点乘。</p>
<figure class="highlight matlab"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">clc;</span><br><span class="line">A=[<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>;<span class="number">4</span>,<span class="number">5</span>,<span class="number">6</span>];</span><br><span class="line">B=[<span class="number">7</span>,<span class="number">8</span>,<span class="number">9</span>;<span class="number">10</span>,<span class="number">11</span>,<span class="number">12</span>];</span><br><span class="line"><span class="keyword">try</span></span><br><span class="line">    C=A*B;</span><br><span class="line"><span class="keyword">catch</span></span><br><span class="line">    C=A.*<span class="number">8</span>;</span><br><span class="line"><span class="keyword">end</span></span><br></pre></td></tr></table></figure></li>
</ul>

            <!--[if lt IE 9]><script>document.createElement('audio');</script><![endif]-->
            <audio id="audio" loop="1" preload="auto" controls="controls" data-autoplay="false">
                <source type="audio/mpeg" src="">
            </audio>
            
                <ul id="audio-list" style="display:none">
                    
                        
                            <li title="0" data-url="http://link.hhtjim.com/163/425570952.mp3"></li>
                        
                    
                        
                            <li title="1" data-url="http://link.hhtjim.com/163/425570952.mp3"></li>
                        
                    
                </ul>
            
        </div>
        
        
    <div id="gitalk-container" class="comment link"
		data-enable="false"
        data-ae="false"
        data-ci=""
        data-cs=""
        data-r=""
        data-o=""
        data-a=""
        data-d="false"
    >查看评论</div>


    </div>
    
</div>


    </div>
</div>
</body>


<script src="//lib.baomitu.com/jquery/1.8.3/jquery.min.js"></script>
<script src="/js/plugin.js"></script>
<script src="/js/typed.js"></script>
<script src="/js/diaspora.js"></script>


<link rel="stylesheet" href="/photoswipe/photoswipe.css">
<link rel="stylesheet" href="/photoswipe/default-skin/default-skin.css">


<script src="/photoswipe/photoswipe.min.js"></script>
<script src="/photoswipe/photoswipe-ui-default.min.js"></script>


<!-- Root element of PhotoSwipe. Must have class pswp. -->
<div class="pswp" tabindex="-1" role="dialog" aria-hidden="true">
    <!-- Background of PhotoSwipe. 
         It's a separate element as animating opacity is faster than rgba(). -->
    <div class="pswp__bg"></div>
    <!-- Slides wrapper with overflow:hidden. -->
    <div class="pswp__scroll-wrap">
        <!-- Container that holds slides. 
            PhotoSwipe keeps only 3 of them in the DOM to save memory.
            Don't modify these 3 pswp__item elements, data is added later on. -->
        <div class="pswp__container">
            <div class="pswp__item"></div>
            <div class="pswp__item"></div>
            <div class="pswp__item"></div>
        </div>
        <!-- Default (PhotoSwipeUI_Default) interface on top of sliding area. Can be changed. -->
        <div class="pswp__ui pswp__ui--hidden">
            <div class="pswp__top-bar">
                <!--  Controls are self-explanatory. Order can be changed. -->
                <div class="pswp__counter"></div>
                <button class="pswp__button pswp__button--close" title="Close (Esc)"></button>
                <button class="pswp__button pswp__button--share" title="Share"></button>
                <button class="pswp__button pswp__button--fs" title="Toggle fullscreen"></button>
                <button class="pswp__button pswp__button--zoom" title="Zoom in/out"></button>
                <!-- Preloader demo http://codepen.io/dimsemenov/pen/yyBWoR -->
                <!-- element will get class pswp__preloader--active when preloader is running -->
                <div class="pswp__preloader">
                    <div class="pswp__preloader__icn">
                      <div class="pswp__preloader__cut">
                        <div class="pswp__preloader__donut"></div>
                      </div>
                    </div>
                </div>
            </div>
            <div class="pswp__share-modal pswp__share-modal--hidden pswp__single-tap">
                <div class="pswp__share-tooltip"></div> 
            </div>
            <button class="pswp__button pswp__button--arrow--left" title="Previous (arrow left)">
            </button>
            <button class="pswp__button pswp__button--arrow--right" title="Next (arrow right)">
            </button>
            <div class="pswp__caption">
                <div class="pswp__caption__center"></div>
            </div>
        </div>
    </div>
</div>






</html>
